package com.flowerSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.flowerSystem.entity.*;
import com.flowerSystem.mapper.CouponMapper;
import com.flowerSystem.mapper.OrderlistMapper;
import com.flowerSystem.service.OrderlistService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flowerSystem.util.PageResult;
import com.flowerSystem.util.ResponseResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 鲜花遇上牛战士
 * @since 2023-08-28
 */
@Service
public class OrderlistServiceImpl extends ServiceImpl<OrderlistMapper, Orderlist> implements OrderlistService {
    @Resource
    private OrderlistMapper orderlistMapper;
    @Resource
    private CouponMapper couponMapper;

    @Override
    public PageResult orderManagerAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<OrderListManager> page = orderlistMapper.orderManagerAll();
        return new PageResult(page.getTotal(), page.getResult());

    }

    @Override
    public IPage<OrderListManager> productInformPrepaid(com.baomidou.mybatisplus.extension.plugins.pagination.Page<OrderListManager> orderListManagerPage) {
        return orderlistMapper.productInformPrepaid(orderListManagerPage);
    }

    @Override
    public IPage<OrderListManager> productInformFinish(com.baomidou.mybatisplus.extension.plugins.pagination.Page<OrderListManager> page) {
        return orderlistMapper.productInformFinish(page);
    }

    @Override
    public int productInformTake(int id) {
        return orderlistMapper.productInformTake(id);
    }

    @Override
    public int productInformDelivery(int id) {
        return orderlistMapper.productInformDelivery(id);
    }

    @Override
    public OrderListManager orderListFinishLike(String orderNumber) {
        return orderlistMapper.orderListFinishLike(orderNumber);
    }

//    ----------------------------------------------------------------------------------------------------
@Override
public Order getOrderList(Order order) {
    String time=new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date());
    String date = String.valueOf(new Date().getTime());
    for (int i=0;i<order.getAllData().size();i++){
        Orderlist orderlist = new Orderlist();
        orderlist.setOrderTime(time);
        orderlist.setOrderNumber(date);
        orderlist.setConsignee(order.getName());
        orderlist.setConsigneePhone(order.getTel());
        orderlist.setConsigneeDetailAddress(order.getAddress());
        //用户id(使用token获取)
        orderlist.setUserId(order.getUserId());
        Productinformation productInformation = order.getAllData().get(i);
        orderlist.setProductInformationId(productInformation.getId());
        orderlist.setSalesNumber(productInformation.getAmount());
        orderlist.setActuallyMoney(order.getRealPrice());
        orderlist.setOrderStatus(order.getState());
        orderlist.setCouponId(order.getCouponId());
        orderlist.setSumMoney(order.getOldPrice());
        orderlistMapper.insert(orderlist);
    }
    order.setOrderNumber(date);
//       orderlists.forEach(oder -> orderlistMapper.insert(oder));
    return order;
}

    @Override
    public int deleteOrderList(String order_number ) {
        return orderlistMapper.deleteOderListByOredrNumber(order_number);
    }

    @Override
    public void updateOrderList(Orderlist orderlist) {
        orderlistMapper.updateOrderList(orderlist);
    }

    @Override
    public List<OrderPermition> getOrderListbyUser(int userId) {
        //查询当前用户的所有订单
        List<Orderlist> orderlists = orderlistMapper.getOrderListbyUser(userId);
        //
        Orderlist orderlist = new Orderlist();
        //创建商品信息表集合
        List<Productinformation> productinformations = new ArrayList<>();
        //创建返回对象集合
        List<OrderPermition> orderPermitionList = new ArrayList<>();
        //创建返回对象
        OrderPermition orderPermition = new OrderPermition();
        //创建订单编号变量
        String number = orderlists.get(0).getOrderNumber();
        for (int i=0;i<orderlists.size();i++){
            orderlist=orderlists.get(i);
            if (orderlists.get(i).getOrderNumber()!=null && !(orderlist.getOrderNumber().equals(number))){
                orderPermition.setProductinformation(productinformations);
                orderPermitionList.add(orderPermition);
                number=orderlist.getOrderNumber();
                productinformations = new ArrayList<>();
                orderPermition = new OrderPermition();
            }
            Productinformation productinformation= orderlistMapper.getProductinformationsById(orderlists.get(i).getProductInformationId());
            productinformations.add(productinformation);
            productinformation.setOrderCount(orderlist.getSalesNumber());
            orderPermition.setCoupon(couponMapper.selectById(orderlist.getCouponId()));
            orderPermition.setOrderNumber(orderlist.getOrderNumber());
            orderPermition.setOrderStatus(orderlist.getOrderStatus());
            orderPermition.setConsignee(orderlist.getConsignee());
            orderPermition.setOrderTime(orderlist.getOrderTime());
            orderPermition.setActuallyMoney(orderlist.getActuallyMoney());
            orderPermition.setDeliverTime(orderlist.getDeliverTime());
            orderPermition.setConsigneePhone(orderlist.getConsigneePhone());
            orderPermition.setSalesNumber(orderlist.getSalesNumber());
            orderPermition.setSumMoney(orderlist.getSumMoney());
            orderPermition.setConsigneeDetailAddress(orderlist.getConsigneeDetailAddress());
            System.out.println(i);
        }
        orderPermition.setProductinformation(productinformations);
        orderPermitionList.add(orderPermition);
        return orderPermitionList;
    }

    @Override
    public void setOrderListTime(String orderNumber) {
        String time=new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date());
        orderlistMapper.updateOrderListByTime(orderNumber,time);
    }

    @Override
    public OrderPermition getOrderByOrderNumber(String orderNumber) {
        List<Orderlist> orderlists = orderlistMapper.getOrderByOrderNumber(orderNumber);
        //创建商品信息表集合
        List<Productinformation> productinformations = new ArrayList<>();
        //创建返回对象集合
        OrderPermition orderPermition = new OrderPermition();
        for (int i=0;i<orderlists.size();i++){
            Productinformation productinformation= orderlistMapper.getProductinformationsById(orderlists.get(i).getProductInformationId());
            productinformation.setOrderCount(orderlists.get(i).getSalesNumber());
            productinformations.add(productinformation);
            orderPermition.setOrderStatus(orderlists.get(i).getOrderStatus());
            orderPermition.setOrderTime(orderlists.get(i).getOrderTime());
            orderPermition.setOrderNumber(orderlists.get(i).getOrderNumber());
            orderPermition.setSalesNumber(orderlists.get(i).getSalesNumber());
            orderPermition.setConsignee(orderlists.get(i).getConsignee());
            orderPermition.setConsigneeDetailAddress(orderlists.get(i).getConsigneeDetailAddress());
            System.out.println("查询优惠券");
            orderPermition.setCoupon(couponMapper.selectById(orderlists.get(i).getCouponId()));
            System.out.println("查询到优惠群");
            orderPermition.setConsigneePhone(orderlists.get(i).getConsigneePhone());
            orderPermition.setActuallyMoney(orderlists.get(i).getActuallyMoney());
            orderPermition.setSumMoney(orderlists.get(i).getSumMoney());
            orderPermition.setId(orderlists.get(i).getId());


        }
        orderPermition.setProductinformation(productinformations);
        return orderPermition;


    }

}
